//
//  ModifyViewController.m
//  随便点
//
//  Created by MWJ on 15/3/11.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "ModifyViewController.h"
#import "AppDelegate.h"
#import "MembersViewController.h"
#define Kmodify @"http://www.51anys.com:8080/waimaiwang/user/editUser"
@interface ModifyViewController (){
    AppDelegate *app;
    NSMutableArray *array;
    NSString *str;
    NSDictionary *dic;
    UIAlertView *_alert;
}

@end

@implementation ModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    str = app._id;
}
-(BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
-(void)ForCharacter{
    if ([self validateEmail:self.emill.text]==NO||self.name==0||self.address==0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"姓名地址错误或者邮箱格式不正确" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        [self judge];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        _alert = alert;
        [alert show];
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        MembersViewController *men = [self.storyboard instantiateViewControllerWithIdentifier:@"membersVC"];
        [self.navigationController pushViewController:men animated:YES];
    }
}
-(void)judge{
    NSURL *url=[NSURL URLWithString:Kmodify];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"userId=%@&name=%@&email=%@&address=%@",str,self.name.text,self.emill.text,self.address.text];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        array = [NSMutableArray array];
        array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:array waitUntilDone:YES];
    }];
}
-(void)refreshData:(NSArray *)sender{
     }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)determine:(id)sender {
    [self ForCharacter];
}

- (IBAction)cancel:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.name resignFirstResponder];
    [self.emill resignFirstResponder];
    [self.address resignFirstResponder];
}
@end
